import jieba
import re
from collections import Counter


def save_words(filename, context):
    file = open(filename, 'w')
    file.write(context)


def cut_words(filename, count):
    cutted_words = ''
    words = ''
    for line in open(filename, encoding='gb18030'):
        line.strip('\n')
        line = re.sub("[A-Za-z0-9\：\·\—\，\。\“ \”]", "", line)
        seg_list = jieba.cut(line, cut_all=False)
        cutted_words += (" ".join(seg_list))
    all_words = cutted_words.split()
    c = Counter()
    for x in all_words:
        if len(x) > 1 and x != '\r\n':
            c[x] += 1

    for (k, v) in c.most_common(5):
        words = words + str(k) + '：' + str(v) + '\n'

    return words


def main():
    save_words('output.txt', cut_words('test.txt', 5))


main()
